import nmap3
import fastapi
from fastapi_mcp import FastApiMCP
api_app = fastapi.FastAPI()

@api_app.get("/scan/{ip}/{comman_args}")
async def scan(ip: str, comman_args: str)-> dict:
    """
    使用nmap进行端口扫描

    Args:
        ip (str): 要扫描的IP地址
        comman_args (str): nmap命令行参数
    Returns:
        dict: 扫描结果
    """
    nm = nmap3.Nmap()
    result = nm.scan_top_ports(ip, args=comman_args)
    return result

# A separate app for the MCP server
mcp_app = fastapi.FastAPI()

# Create MCP server from the API app
mcp = FastApiMCP(
    api_app,
    base_url="http://localhost:8001",  # The URL where the API app will be running
)

# Mount the MCP server to the separate app
mcp.mount(mcp_app)

# Now you can run both apps separately:
# uvicorn main:api_app --host localhost --port 8001
# uvicorn main:mcp_app --host localhost --port 8000